package com.sakura.mall.product.skuInfo.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.product.skuInfo.domain.entity.SkuInfoEntity;
import com.sakura.mall.product.skuInfo.domain.request.AddSkuInfoRequest;
import com.sakura.mall.product.skuInfo.domain.request.UpdateSkuInfoRequest;
import com.sakura.mall.product.skuInfo.domain.request.SkuInfoPageRequest;
import com.sakura.mall.product.skuInfo.domain.vo.SkuInfoVo;
import com.sakura.mall.product.skuInfo.mapper.SkuInfoMapper;
import com.sakura.mall.product.skuInfo.service.SkuInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * sku信息 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfoEntity> implements SkuInfoService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddSkuInfoRequest request) {
        // todo 其他校验逻辑
        SkuInfoEntity skuInfo = request.convert(SkuInfoEntity.class);
        this.save(skuInfo);
    }

    @Override
    public Page<SkuInfoVo> queryPage(SkuInfoPageRequest request) {
        IPage<SkuInfoEntity> qp = request.toPage();
        LambdaQueryWrapper<SkuInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<SkuInfoVo> ret = qp.convert(entity -> entity.convert(SkuInfoVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateSkuInfoRequest request) {
        SkuInfoEntity entity = request.convert(SkuInfoEntity.class);
        entity.setSkuId(id);
        this.updateById(entity);
    }

}

